package com.hsy.sunflowerbao.home.ui

import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.flux.action.RxStoreChange
import com.flux.utils.GlideUtil
import com.flux.utils.GsonUtil
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.base.data.TagData
import com.hsy.sunflowerbao.base.data.UserData
import com.hsy.sunflowerbao.base.ui.activity.BaseActivityWithToolbar
import com.hsy.sunflowerbao.home.action.HomeActionCreator
import com.hsy.sunflowerbao.home.store.DetailStore
import com.hsy.sunflowerbao.utils.CommonUtils
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout
import kotlinx.android.synthetic.main.activity_detail.*

/**
 * Created on 2018/3/29.
 *
 * @author Yang
 */
class DetailActivity : BaseActivityWithToolbar<DetailStore>() {

    /**
     * 事件产生器
     */
    private val creator = HomeActionCreator(dispatcher, manager)
    /**
     * 数据缓存器
     */
    override var store = DetailStore(dispatcher)
    /**
     * 专家库适配器
     */
    private lateinit var adapter: CouponAdapter
    /**
     * 用户ID
     */
    private lateinit var user: UserData
    /**
     * 使用的优惠券编号
     */
    private var position = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)
        setTitle(R.string.title_detail)

        user = GsonUtil.fromJson(activityStartExtra!![0], UserData::class.java)
        GlideUtil.loadCircle(user.avatar, userAvatar, R.drawable.default_avatar, activity = this)
        userName.text = getString(R.string.txt_home_user_name, user.nick_name)
        userPhone.text = getString(R.string.txt_home_user_phone, user.user_name)
        userMoney.text = user.money
        userCoupon.text = user.xf_num
        userScore.text = user.score
        adapter = CouponAdapter(this, user.quans)
        recycle.layoutManager = LinearLayoutManager(recycle.context)
        recycle.adapter = adapter

        if (user.tags.isNotEmpty()) when (user.tags.size) {
            1 -> {
                tag1.text = user.tags[0].name
                tag2.visibility = View.GONE
                tag3.visibility = View.GONE
                tag4.visibility = View.GONE
                tagMore.visibility = View.GONE
            }
            2 -> {
                tag1.text = user.tags[0].name
                tag2.text = user.tags[1].name
                tag3.visibility = View.GONE
                tag4.visibility = View.GONE
                tagMore.visibility = View.GONE
            }
            3 -> {
                tag1.text = user.tags[0].name
                tag2.text = user.tags[1].name
                tag3.text = user.tags[2].name
                tag4.visibility = View.GONE
                tagMore.visibility = View.GONE
            }
            4 -> {
                tag1.text = user.tags[0].name
                tag2.text = user.tags[1].name
                tag3.text = user.tags[2].name
                tag4.text = user.tags[3].name
                tagMore.visibility = View.GONE
            }
            else -> {
                tag1.text = user.tags[0].name
                tag2.text = user.tags[1].name
                tag3.text = user.tags[2].name
                tag4.text = user.tags[3].name
            }
        }

        tagMore.setOnClickListener {
            val flowLayout = TagFlowLayout(this)
            flowLayout.adapter = object : TagAdapter<TagData>(user.tags) {
                override fun getView(parent: FlowLayout?, position: Int, t: TagData): View {
                    val tag = LayoutInflater.from(this@DetailActivity).inflate(R.layout.item_tag_dialog, parent, false) as TextView
                    tag.text = t.name
                    return tag
                }
            }
            AlertDialog.Builder(this)
                    .setView(flowLayout)
                    .setCancelable(true)
                    .show()
                    .setCanceledOnTouchOutside(true)
        }
        nextStep.setOnClickListener { _ -> startActivityForResult(ScoreActivity::class.java, 0, GsonUtil.toJson(user)) }
    }

    fun checkCoupon(id: String, position: Int) {
        CommonUtils.showProgressDialog(this)
        creator.checkCoupon(id, user.id)
        this.position = position
    }

    /**
     * 数据刷新响应
     * @param change 需要响应的改变
     */
    override fun onRxStoreChanged(change: RxStoreChange) {
        super.onRxStoreChanged(change)
        if (change.storeId == store.tag) when (change.type) {
            ActionType.CHECK_COUPON -> {
                CommonUtils.hideProgressDialog()
                if (change.success) {
                    user.quans.removeAt(position)
                    adapter.items = user.quans
                    adapter.notifyDataSetChanged()
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Array<String>?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) finish()
    }
}